package org.learn.spring5.service.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//增强类，编写增强的方法
@Component
@Aspect
@Order(1)
public class UserServiceProxy {

    //相同的切入点抽取
    @Pointcut(value = "execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    public void pointCunt(){


    }

    //前置通知
    //@Before(value ="execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    @Before(value = "pointCunt()")
    public void before(){
    System.out.println("before");
}

    //后置通知
    //@After(value ="execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    @After(value = "pointCunt()")
    public void after(){
    System.out.println("After");
}
    //最终通知
    //@AfterReturning(value ="execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    @AfterReturning(value = "pointCunt()")
    public void afterReturning(){
        System.out.println("afterReturning");
    }
    //异常通知
    //@AfterThrowing(value ="execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    @AfterThrowing(value = "pointCunt()")
    public void afterThrowing(){

        System.out.println("AfterThrowing");
    }
    //环绕通知
    //@Around(value ="execution(* org.learn.spring5.service.impl.UserServiceImpl.add(..))")
    @Around(value = "pointCunt()")
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("环绕之前");
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("环绕之后");
    }

}
